// table部分数据与方法混入
export default {
  props: {
    checkedList: {
      type: Array,
      default: () => [] // 选中项
    },
    primaryKey: {
      type: String,
      default: 'id'
    }
  },
  data () {
    return {
      loading: false,
      currentPage: 1,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      query: {}, // 查询条件（非分页相关条件）
      list: [], // 数据缓存
      checkList: JSON.parse(JSON.stringify(this.checkedList)),
      manualPages: false
    }
  },
  methods: {
    // 查询表格数据
    getList () {
      return new Promise((resolve, reject) => {
        if (!this.api) reject(new Error())
        this.loading = true
        this.api({
          currentPage: this.currentPage,
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          ...this.query
        })
          .then(res => {
            this.loading = false
            if (res.code === 1) {
              if (this.manualPages) {
                this.list = res.datas
                this.total = this.list.length
              } else {
                this.list = res.datas.datas || res.datas.list
                this.total = res.datas.total
              }
              // 默认选中项
              setTimeout(() => this.setChecked())
            } else {
              this.list = []
              this.total = 0
            }
            resolve(res)
          })
          .catch(() => {
            reject(new Error())
            this.loading = false
          })
      })
    },
    // 搜索
    search () {
      this.currentPage = 1
      this.getList()
    },
    // 排序
    sortChange ({ prop, order }, isSearch = false) {
      this.query[prop] = order
      if (isSearch) this.search()
    },
    // 筛选
    filterChange (filters, resultType = 'string', isSearch = true) {
      if (resultType === 'string') {
        for (let key in filters) {
          this.query[key] = filters[key].join(',')
        }
      } else if (resultType === 'array') {
        for (let key in filters) {
          this.query[key] = filters[key]
        }
      }
      // 重新获取数据
      if (isSearch) this.search()
    },
    setChecked () {
      const primaryKeys = this.checkList.map(it => it[this.primaryKey])
      // 手动分页
      if (this.manualPages) {
        this.renderList.forEach(item => {
          if (primaryKeys.includes(item[this.primaryKey])) { this.$refs.table.toggleRowSelection(item, true) }
        })
      } else {
        this.list.forEach(item => {
          if (primaryKeys.includes(item[this.primaryKey])) {
            this.$refs.table.toggleRowSelection(item, true)
          }
        })
      }
    },
    selectOne (selection, row) {
      const isCheck = selection.some(
        it => it[this.primaryKey] === row[this.primaryKey]
      )
      // 选中
      if (isCheck) {
        this.checkList.push(row)
      } else {
        this.checkList = this.checkList.filter(
          it => it[this.primaryKey] !== row[this.primaryKey]
        )
      }
    },
    selectAll (selection) {
      const isCheck = !!selection.length
      if (isCheck) {
        const keys = this.checkList.map(it => it[this.primaryKey])
        selection.forEach(item => {
          if (keys.includes(item[this.primaryKey])) this.checkList.push(item)
        })
      } else {
        const primaryKeys = this.manualPages
          ? this.renderList.map(it => it[this.primaryKey])
          : this.list.map(it => it[this.primaryKey])
        this.checkList = this.checkList.filter(
          it => !primaryKeys.includes(it[this.primaryKey])
        )
      }
    },
    selChanged (selection) {
      this.checkList = selection
    },
    getRowKey (row) {
      return row[this.primaryKey]
    }
  }
}
